package tto.gio.next;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import support.Variables;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class LogReader {
	protected LogReader()
	{
		mStop = false;
	}
	private static LogReader mReader;
	private static Context mContext;
	private static Monitor mMonitor;
	private Thread mThread;
	private boolean mStop;
	private String tag="tto.gio.next";
	public static String APP_INTENT ="tto.gio.next.intent.action.LOGUPDATE";
	public static void init(Context context, Monitor monitor){
		mContext = context;
		mMonitor = monitor;
	}
	public static LogReader getReader()
	{
		if(mReader == null)
			mReader = new LogReader();
		return mReader;
	}
	public void start()
	{
		mThread = new Thread(worker);
		mThread.setDaemon(true);
		mThread.start();
	}
	public void stop()
	{
		mStop = true;
	}
	Runnable worker = new Runnable()
	{		
		public void run()
		{
			try{
				runLog();
			}catch(Exception ee)
			{
				Log.e(tag,ee.toString());
				ee.printStackTrace();
			}
		}		
	};
	public String GetTopPackage() {
		ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> info;
		info = activityManager.getRunningTasks(1);
		//for (Iterator iterator = info.iterator(); iterator.hasNext();) {
		//	RunningTaskInfo runningTaskInfo = (RunningTaskInfo) iterator.next();
		//	Log.d(tag,runningTaskInfo.topActivity.getClassName().toString());
		//	Log.d(tag,runningTaskInfo.topActivity.getClassName().toString());
		//}
		RunningTaskInfo topTask = (RunningTaskInfo)info.get(0);
		//Log.d(tag,topTask.topActivity.getPackageName().toString());
		//Log.d(tag,topTask.topActivity.getClassName().toString());
		
		
		return topTask.topActivity.getPackageName();
		
		
	}
	private void CheckNewActivity()
	{
		Intent intent = new Intent();
		intent.setAction(APP_INTENT);
		String preString = "",cur;
		while(!mStop)
		{
			cur = GetTopPackage();
			if(preString.compareTo(cur) != 0)
			{
				preString = cur;
				Log.d(tag,cur);
				mContext.sendBroadcast(intent);
			}
			else
			{
				try{
					Thread.sleep(5000);
				}catch(Exception ee)
				{
					ee.printStackTrace();
				}
			}
		}
		
	}
	private void runLog() throws InterruptedException 
	{
		// TODO Auto-generated method stub
		if(!Variables.isOTA)
		{
			CheckNewActivity();
			return;
		}
		
		
		Process process = null;
		Intent intent = new Intent();
		intent.setAction(APP_INTENT);
		try
		{
			Runtime.getRuntime().exec("/system/bin/logcat -c");
			process = Runtime.getRuntime().exec(
					"/system/bin/logcat -s ActivityManager:I");
		}
		catch(IOException e)
		{
			Log.e(mContext.getPackageName(),e.toString());
		}
		BufferedReader reader = null;
		
		try
		{
			InputStreamReader stream = new InputStreamReader(process.getInputStream());
			reader = new BufferedReader(stream, 1024);
			
			String line="";
			
			while (!mStop) {
				try{
					if(reader.ready()){
						line = reader.readLine();
						Log.i("RTT",line);
					}
				}catch(IOException ee)
				{
					ee.printStackTrace();
				}
				if(line != null)
				{
					if(line.contains("mDVFSLock.release()"))
					{
						//mMonitor.onReceive(mContext, intent);
						mContext.sendBroadcast(intent);
						Log.d(tag,"data : "+line);
					}
					else if(line.contains("START"))
					{
						//mMonitor.onReceive(mContext, intent);
						mContext.sendBroadcast(intent);
						Log.d(tag,"data : "+line);
					}
					else if(line.contains("Starting"))
					{
						//mMonitor.onReceive(mContext, intent);
						mContext.sendBroadcast(intent);
						Log.d(tag,"data : "+line);
					}
					else if(line.contains("mDVFSHelper.release"))
					{
						mContext.sendBroadcast(intent);
						Log.d(tag,"data : "+line);
					}
					//Log.d(tag,"data : "+line);
				}
				else
					Thread.sleep(5000);
							
				//intent.putExtra("pid", value);
				//mMonitor.onReceive(mContext , intent);
			}

		}
		catch(Exception e)
		{
			Log.e(tag,e.toString());			
		}
		finally
		{
			try
			{
				reader.close();
			}catch (IOException e)
			{
				// TODO Auto-generated catch block
				Log.e(tag,e.toString());
			}			
			process.destroy();
			process = null;
			reader = null;
			mStop = false;
		}
	}
}
